/**
 * Project Name:dsrs-systemservice-service
 * File Name:UserSecurityServiceImpl.java
 * Package Name:com.bwda.dsrs.systemservice.service.impl;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 */
package com.bwda.dsrs.systemservice.service.impl;

import com.bwda.dsrs.systemservice.domain.condition.usersecurity.UserSecurityCondition;
import com.bwda.dsrs.systemservice.domain.exception.SystemServiceException;
import com.bwda.dsrs.systemservice.domain.po.usersecurity.UserSecurityPo;
import com.bwda.dsrs.systemservice.mapper.UserSecurityMapper;
import com.bwda.dsrs.systemservice.service.UserSecurityService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
/**
 * 用户安全策略接口实现类
 *
 * @author yanyuemei
 * @date 2019/4/27
 */
/**
 * 用户-安全策略关联关系
 *
 * @author liangjianyong
 * @date 2019/4/22
 */
@Service
public class UserSecurityServiceImpl extends AbstractBaseServiceImpl<UserSecurityPo, UserSecurityCondition, UserSecurityMapper> implements UserSecurityService {

    @Override
    public UserSecurityPo querySecurityByUserCode(String userCode) throws SystemServiceException{
        UserSecurityCondition condition = new UserSecurityCondition();
        condition.setUserCode(userCode);
        List<UserSecurityPo> po = this.queryList(condition);
        //安全策略不存在，默认生成一条安全策略
        if(po.isEmpty()){
            UserSecurityPo userSecurityPo = new UserSecurityPo();
            userSecurityPo.setUserCode(userCode);
            userSecurityPo.setEffTime(new Date());
            userSecurityPo.setCreateTime(new Date());
            //生成用户的安全策略
            this.insert(userSecurityPo);
            return userSecurityPo;
        }
        if(po.size()>1){
            throw new SystemServiceException("用户安全策略不唯一");
        }
        return po.get(0);
    }
}